<?php

namespace core\traits;

use support\View;
use Webman\Http\Response as HttpResponse;

trait Response
{

    public function success(
        array $data = []
    ) {
        if (request()->isAjax()) {
            return json([
                'code' => 0,
                'data' => $data
            ]);
        } else {
            return response('');
        }
    }

    public function error(
        string $message = '系统错误',
        int $status = 200,
        int $code = -1
    ): HttpResponse {
        if (request()->isAjax()) {
            return json([
                'code'    => $code,
                'message' => $message
            ]);
        } else {
             $template = $status === 200 ? 'error' : '404';
            return view('/template/' . $template, ['message' => $message])->withStatus($status);
        }
    }
}
